package wili.insertCode;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import wili.c.File9a;
import wili.c.src.ClsObj;
import wili.c.src.JaClassInds;
import wili.c.src.JaFileInds;

/**
 * @author weila 2022年6月23日
 */
public class JfilePart {
	public final List<ClassPart> clsParts = new ArrayList<>(3);

	public final String s0, src_blankComment;

	public final ClsObj cls;

	public String newSt;

	final File root, file;

	public String toString() {
		return file.getAbsolutePath();
	}

	public static JfilePart newInst(File root, File file) {
		try {
			if (file.getName().equals("BoODOCImpl.java"))
				Coms2.pause();
			String s0 = FileUtils.readFileToString(file, Charset.defaultCharset());
			if (s0.startsWith(Coms2.PreMark)) {
				Coms2.log(" Already patched file " + file.getName() + " in " + file.getParentFile());
				return null;
			} else {//生成 filePart前备份。
				File bfile = Coms2.getBackupFile(file);
				if (!bfile.exists() || bfile.lastModified() != file.lastModified()) {
					if (bfile.exists())
						Coms2.pause();
					if (!File9a.copyFile(file, bfile))
						return null;
				}
			}
			JfilePart ret = new JfilePart(s0, root, file);
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private JfilePart(String s0, File jroot, File jfile) {
		Coms2.log(-1, " JfilePart, file ", jfile.getName());
		Cont cont = new Cont();
		this.root = jroot;
		this.file = jfile;
		this.s0 = s0;
		this.cls = new ClsObj(jroot, jfile, Coms2.getClassFromJava);
		JaFileInds ja = new JaFileInds(cls);
		this.src_blankComment = ja.src_blankComment;
		List<JaClassInds> cc = ja.sonMap.getClassList();
		if (cc == null)
			return;
		for (JaClassInds inds : cc) {
			inds.loadSons(0);//no deep into.
			clsParts.add(new ClassPart(inds, this, cont));
		}
	}

	public List<MethodPart> findMethod() {
		return null;
	}

	InsertStrings insert;

	public void addInsert(int from, String str, int coverNum) {
		if (insert == null)
			insert = new InsertStrings();
		insert.addInsert(from, str, coverNum);
	}

	File backupFile;

	public void saveFile() {
		if (insert == null)
			return;
		//String newSt = Str9a.insertMapIntoString(insertMap___, s0);
		String newSt = insert.insertInto(s0);
		newSt = Coms2.PreMark + ", " + Coms2.dateTimeStrOfNow() + "\n" + newSt;
		if (backupFile == null) {
			backupFile = Coms2.getBackupFile(file);
		}
		try {
			if (backupFile.exists() && backupFile.lastModified() == file.lastModified()) {
			} else {//before insert , overwrite not same backup file.
				if (!File9a.copyFile(file, backupFile)) {
					return;
				}
			}
			++allNum;
			if ("".isEmpty() && file.canWrite()) {
				FileUtils.write(file, newSt, Charset.defaultCharset());
				++patchedNum;
				Coms2.log(" " + allNum + ", patching file: " + file.getName() + ", backupFile: "
						+ backupFile.getAbsolutePath());
			} else {
				Coms2.log("\n CanNot write:" + file.getAbsolutePath());
			}
		} catch (IOException e) {
			Coms2.err("\n Failed patching: " + file.getName() + ", at " + file.getAbsolutePath());
			e.printStackTrace();
		}
	}

	public static int allNum, patchedNum;
}
